package com.micookie.v2ex.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.micookie.v2ex.R;
import com.micookie.v2ex.model.bean.Topic;
import com.micookie.v2ex.ui.TopicDetailActivity;
import com.micookie.v2ex.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.badgeview.BGABadgeRelativeLayout;


/**
 * Created by Administrator on 2016-10-25.
 */

public class TopicListAdapter extends RecyclerView.Adapter<TopicListAdapter.TopicViewHolder> {

    private Context mContext;

    public List<Topic> getDataList() {
        return mDataList;
    }

    private List<Topic> mDataList;

    public TopicListAdapter(Context context, List<Topic> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }


    @Override
    public TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TopicViewHolder(LinearLayout.inflate(mContext, R.layout.item_topic, null));
    }

    @Override
    public void onBindViewHolder(TopicViewHolder holder, int position) {
        Topic topic = mDataList.get(position);
        holder.setData(topic);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    /**
     * TopicViewHolder
     */
    class TopicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        @BindView(R.id.item_topic_cv)
        CardView mTopicCv;
        @BindView(R.id.item_topic_reply_num_bagrl)
        BGABadgeRelativeLayout mReplyNumBGA;
        @BindView(R.id.item_topic_avatar_sdv)
        SimpleDraweeView mAvatarSdv;
        @BindView(R.id.item_topic_title_tv)
        TextView mTitleTv;
        @BindView(R.id.item_topic_name_tv)
        TextView mNameTv;
        @BindView(R.id.item_topic_node_tv)
        TextView mNodeTv;
        @BindView(R.id.item_topic_otherInfo_tv)
        TextView mOtherInfoTv;

        private Topic topicData;

        public TopicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            mTopicCv.setOnClickListener(this);
            mAvatarSdv.setOnClickListener(this);
            mTitleTv.setOnClickListener(this);
            mNameTv.setOnClickListener(this);
            mNodeTv.setOnClickListener(this);
        }

        public void setData(Topic topic) {
            topicData = topic;
            Uri uri = Uri.parse(topic.getMember().getAvatar());
            mAvatarSdv.setImageURI(uri);
            mTitleTv.setText(topic.getTitle());
            //如果节点信息为空,则隐藏不显示
            boolean isNodeInfoEmpty = topic.getNode().getName() == null || topic.getNode().getName().isEmpty();
            if (isNodeInfoEmpty) {
                mNodeTv.setVisibility(View.GONE);
            } else {
                mNodeTv.setText(topic.getNode().getName());
            }
            String name = (isNodeInfoEmpty ? "" : " • ") + topic.getMember().getUsername();
            mNameTv.setText(name);
            mOtherInfoTv.setText(topic.getOtherInfo());
            mReplyNumBGA.showTextBadge(topic.getReplies() + "");
        }


        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.item_topic_cv:
                    //ToastUtils.show("topic");
                    TopicDetailActivity.startActivity(mContext, topicData);
                    break;
                case R.id.item_topic_title_tv:
                    //ToastUtils.show("title");
                    TopicDetailActivity.startActivity(mContext, topicData);
                    break;
                case R.id.item_topic_node_tv:
                    ToastUtils.show("node");
                    break;
                case R.id.item_topic_avatar_sdv:
                    ToastUtils.show("avatar");
                    break;

            }

        }
    }
}
